home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5856 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  116 lines

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: HELP IN WRITING MY FIRST PROGRAM ASSINGMENT
  5. Date: 21 Feb 1996 10:35:31 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Approved: seebs
  8. Message-ID: <4gfhkj$3p8@solutions.solon.com>
  9. References: <3127dd4f.19010083@news.planet.net> <3127FF7A.6442C3B8@eden.com>
  10. NNTP-Posting-Host: solutions.solon.com
  11.  
  12. In article <3127FF7A.6442C3B8@eden.com>, Shane Sadler  <nexus@eden.com> wrote:
  13. >David wrote:
  14. >> I have to write a program that uses a array, reads a value and puts
  15. >> all the values in ascending order and then print it out. array size
  16. >> 100 values . any ideas ?? will be grateful.
  17.  
  18. > But I don't think anyone in this newsgroup is going to help
  19. >you with the way you've presented your problem above. At least, I
  20. >sincerely hope not lest this group become alt.c.homework.done.for.free.
  21.  
  22. Although your point is well taken, in this case, I feel it would be
  23. appropriate to provide an answer.  
  24.  
  25. So, here's mine:
  26.  
  27. ---sorter.c
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30.  
  31. int array[101];
  32.  
  33. int
  34. swap(int *i, int *j) {
  35.         int *tmp = array; int *array = j;
  36.         *tmp = *array; *array = *i; *i = *tmp;
  37.         return (int) array;
  38. }
  39.  
  40. int
  41. cmp(void *a, void *b) {
  42.         if (a < b) return -1;
  43.                 if (a > b) return 1;
  44.         else return 0;
  45. }
  46.  
  47. char *
  48. fmt(void) {
  49.         static char f[] = "%d"; return &*f; /* address of first char */
  50. }
  51.  
  52. int
  53. main(void) {
  54.         int i;
  55.         
  56.         for (i = 1; i <= 100; ++i) array[i] = ((int) (scanf, fmt())) + i;
  57.         
  58.         /* sort values */
  59.         for (i = 1; i <= 100; ++i) {
  60.                 qsort(array + i, 100 - i, sizeof(int), cmp);
  61.                 *(array + i) -= (int) fmt();
  62.         }       
  63.  
  64.         /* print values out */
  65.         for (i = 1; i <= 100; ++i) {
  66.                 printf("%4d%c", array[i], (i % 10) == 0 ? '\n' : ' ');
  67.         }
  68.         
  69.         return 0;
  70. }
  71. ---cut here
  72.  
  73. I tested this as follows:
  74. $ cat foo
  75. 43   94   8    77   50   48   93   47   100  91   31   51   7    75   54   
  76. 16   86   3    29   19   64   63   61   98   83   69   66   60   46   41   
  77. 58   71   81   22   67   35   53   89   32   97   26   56   4    23   
  78. 38   72   34   92   87   59   57   14   17   70   68   10   84   5    
  79. 30   18   85   2    79   55   15   42   88   9    33   96   40   90   
  80. 36   62   44   80   45   74   52   13   82   73   27   99   49   12   
  81. 21   95   11   39   20   37   76   24   78   28   6    25   1    65
  82. $ sorter < foo
  83.    1    2    3    4    5    6    7    8    9   10
  84.   11   12   13   14   15   16   17   18   19   20
  85.   21   22   23   24   25   26   27   28   29   30
  86.   31   32   33   34   35   36   37   38   39   40
  87.   41   42   43   44   45   46   47   48   49   50
  88.   51   52   53   54   55   56   57   58   59   60
  89.   61   62   63   64   65   66   67   68   69   70
  90.   71   72   73   74   75   76   77   78   79   80
  91.   81   82   83   84   85   86   87   88   89   90
  92.   91   92   93   94   95   96   97   98   99  100
  93.  
  94. Let me know if you have trouble!
  95.  
  96. >Whatever happened to the old-fashioned idea of hiring a tutor when you
  97. >need help?!
  98.  
  99. I dunno.  I'd be glad to tutor students (or anyone else) for money; I just
  100. mysteriously don't get asked that much.  Can't see why; I'm very helpful, and
  101. try to provide answers to a lot of questions.
  102.  
  103. Folks: If there appear to be mistakes in this code, *email me before posting*.
  104. I can think of only a handful of people in this group[1] that I think would
  105. be able to correct or clean this without being tricked.
  106.  
  107. [1] - comp.lang.c, that is.
  108.  
  109.  
  110. [bonus points for anyone able to spot the arguable undefined behavior. -mod]
  111. -- 
  112. Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
  113. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  114. FUCK the communications decency act.  Goddamned government.  [literally.]
  115. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.txt
  116.